//*****************************************************************************
//
//	.COM Framework 1.7
//
//	Copyright (C) 2006-2009 Softg Studio All Rights Reserved.
//	
//	Website: http://www.SoftG.net
//
//	author: xu song seng
//
//*****************************************************************************


#include "stdafx.h"
#include "MDIChildWindows.h"
#include "MainFrame.h"
#include ".\mdichildwindows.h"
#include "atlcomcli.h"

// CMDIChildWindows

CMDIChildWindows::CMDIChildWindows(CMainFrame *pMainFrame)
{
	this->m_pMainFrame = pMainFrame;
}

CMDIChildWindows::CMDIChildWindows()
{
}

CMDIChildWindows::~CMDIChildWindows()
{
}

LRESULT CMDIChildWindows::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	DoDataExchange();
	DlgResize_Init();

	InitListCtrl();
	DataToListCtrl();

	return 1;  // Let the system set the focus
}

LRESULT CMDIChildWindows::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	DoDataExchange(TRUE);
	EndDialog(wID);


	return 0;
}

LRESULT CMDIChildWindows::OnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	EndDialog(wID);
	return 0;
}

LRESULT CMDIChildWindows::OnButActivate(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{

	return 0;
}

LRESULT CMDIChildWindows::OnButSave(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{

	return 0;
}


LRESULT CMDIChildWindows::OnIdok(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{

	return 0;
}


int CMDIChildWindows::InitListCtrl(void)
{
	m_wndListCtrl.AddColumn(L"Name", 0);
	m_wndListCtrl.AddColumn(L"Path", 1);

	return 0;
}


void CMDIChildWindows::DataToListCtrl(void)
{
	ATLASSERT(this->m_pMainFrame != NULL);

	HRESULT			hr		= 0;
	long			nCount	= 0;
	CComVariant		varItem;
	CComBSTR		strCaption;
	CComPtr<ITabPageItem>	_pPageItem;
	CComPtr<IMdiContainer>	_pContainer;

	if (NULL == this->m_pMainFrame)
		return ;

	this->m_pMainFrame->get_MdiContainer(&_pContainer);
	_pContainer->get_Count(&nCount);

	for (long nIndex=1; nIndex<=nCount; nIndex++)
	{
		varItem = nIndex;
		hr = _pContainer->GetTabPageItem(varItem, &_pPageItem);
		ASSERT_COM(hr);
		if (S_OK == hr)
		{
			_pPageItem->get_Caption(&strCaption);
			m_wndListCtrl.InsertItem(nIndex, strCaption);
		}

		_pPageItem.Release();
	}


	//HRESULT			hr = 0;
	//ULONG			CeltFetched = 0;
	//CComBSTR		_bstrTitle;
	//int				nItem = 0;


	//pFrame->get_ArtChildFrames((IDispatch**)&_lpChildFrames);
	//hr = pFrame->get__NewEnum((IUnknown**)&lpEnumUnknown);

	//if (SUCCEEDED(hr))
	//{
	//	lpEnumUnknown->Reset();

	//	while (lpEnumUnknown->Next(1, (IUnknown**)&lpChildFrame , &CeltFetched)==S_OK && CeltFetched)
	//	{
	//		lpChildFrame->get_Title(&_bstrTitle);
	//		m_wndListCtrl.InsertItem(nItem, _bstrTitle);
	//		nItem ++;
	//	}

	//	return TRUE;
	//}

	return;
}

LRESULT CMDIChildWindows::OnTbnDropDown(LPNMHDR pnmh)
{
	LPNMTOOLBAR lpnmtb = (LPNMTOOLBAR)pnmh;
	//only the hdr and iItem members of lpnmtb are valid
	//return
	//TBDDRET_DEFAULT The drop-down was handled. 
	//TBDDRET_NODEFAULT The drop-down was not handled. 
	//TBDDRET_TREATPRESSED The drop-down was handled, but treat the button like a regular button

	return TBDDRET_DEFAULT;
}

LRESULT CMDIChildWindows::OnBnClickedActivate(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add your control notification handler code here

	return 0;
}
